
package com.chy.utilsdemo.FileUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 功能： 文件上传工具类 示例代码
 *
 * @author CHY
 */
@Api("文件上传中心")
@RestController
public class FileController {

    @Resource
    private FileUtils fileUtils;

    /**
     * 上传文件
     * @param multipartFile 文件
     * @return
     */
    @PutMapping("/uploadFile")
    @ApiOperation(value = "上传文件", httpMethod = "PUT", notes = "上传文件")
    public String uploadFile(@RequestParam("file") MultipartFile multipartFile) {
        String fileUrl = fileUtils.saveFile(multipartFile);
        return "上传成功，路径为"+fileUrl;
    }

    /**
     * 删除文件
     * @param fileUrl 一般为文件名称：head_A57AF2B824AF45FEA30E247607F9E06C.jpg
     * @return
     */
    @GetMapping("/deleteFile")
    @ApiOperation(value = "删除文件", httpMethod = "GET", notes = "删除文件")
    public String deleteFile(String fileUrl) {
        int delFile = fileUtils.delFile(fileUrl);
        if (delFile > 0 ){
            return "删除成功";
        }else {
            return "删除失败";
        }

    }
}
